package cn.zhuama.org.controller;

import cn.zhuama.basic.util.ExcelUtils;
import cn.zhuama.org.domain.ShopAuditLog;
import cn.zhuama.org.service.IShopService;
import cn.zhuama.org.domain.Shop;
import cn.zhuama.org.query.ShopQuery;
import cn.zhuama.basic.util.AjaxResult;
import cn.zhuama.basic.util.PageList;
import cn.zhuama.org.vo.ShopVo;
import org.apache.catalina.connector.Response;
import org.apache.velocity.runtime.directive.Foreach;
import org.junit.platform.commons.function.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null){
                    shopService.editById(shop);
            }else{
                    shopService.save(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.londOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryData(query);
    }
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return  AjaxResult.me().setMessage("店铺入驻提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("店铺入驻提交失败").setSuccess(false).setMessage(e.getMessage());
        }
    }
    @PostMapping("/audit/reject")
    public AjaxResult  reject(@RequestBody ShopAuditLog log, HttpServletRequest request){
        try {
            shopService.reject(log,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false);
        }
    }
    @PostMapping("/audit/pass")
    public AjaxResult  pass(@RequestBody ShopAuditLog log, HttpServletRequest request){
        try {
            shopService.pass(log,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setSuccess(false);
        }
    }
    @GetMapping("/active/{shopId}")
    public void   active(@PathVariable("shopId")Long id, HttpServletResponse response)throws IOException {
        try {
            shopService.active(id);
            response.sendRedirect("Http://localhost:8081/#/login");
            return;
        } catch (Exception e) {
            e.printStackTrace();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("<script>alert('"+e.getMessage()+"');location.href='http://localhost:8081/#/login';</script>\")</script>");
        }
    }
    @GetMapping("/exportdata")
    public void exportdata(HttpServletResponse response){
        List<Shop> shops = shopService.loadAll();
        ExcelUtils.exportExcel(shops,"店铺汇总表","shops",Shop.class,"shop.xlsx", response);
    }
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> list = ExcelUtils.importExcel(file,0,1,Shop.class);
        System.out.println("导入数据一共【"+list.size()+"】行");
        for (Shop shop : list) {
            System.out.println(shop);
        }
        // 保存到数据库中
        //shopService.saveShops(list);
    }
    @GetMapping("/charts")
    public AjaxResult getCharts(){
        Map<String,Object> charts = shopService.charts();
        AjaxResult me = AjaxResult.me();
        me.setData(charts);
        return  me;
    }

}
